package com.zofer.study.array;

/**
 * 题目：编写一个函数来查找字符串数组中的最长公共前缀。
 */
public class LongestCommonPrefix {
    public static void main(String[] args) {
        String [] ggzfc = {"abcaiup","abcpoio","abca","abc","abcc"};
        System.out.println(longestCommonPrefix(ggzfc));
    }

    public static String longestCommonPrefix (String[] strs) {
        if(strs.length == 0){
            return "";
        }
        // write code here
        String str = strs[0];
        for(int i = 1;i < strs.length;i++){
            while(strs[i].indexOf(str) != 0){
                str = str.substring(0,str.length()-1);
                if(str.length() == 0){
                    return "";
                }
            }
        }
        return str;
    }
}
